package com.lg.wechat.handler;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.lg.wechat.business.wxuser.entity.WxUser;
import com.lg.wechat.business.wxuser.service.WxUserService;
import com.lg.wechat.common.annotation.Clear;

import java.util.Date;
import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
@Clear
public class UnsubscribeHandler extends AbstractHandler {

  @Autowired
  WxUserService wxUserService;
	
  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                  Map<String, Object> context, WxMpService wxMpService,
                                  WxSessionManager sessionManager) {
    String openId = wxMessage.getFromUser();
    this.logger.info("取消关注用户 OPENID: " + openId);
    WxUser wxUser=wxUserService.selectByOpenId(openId);
    if(wxUser!=null){
    	wxUser.setSubscribe(1);
    	wxUser.setUnsubscribeTime(new Date());
    	wxUserService.updateById(wxUser);
    }
    return null;
  }

}
